package com.ylx;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.util.DocumentUtil;
import org.apache.http.util.TextUtils;

public class MyCamelCaseAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {

        final Editor mEditor = e.getData(PlatformDataKeys.EDITOR);

        if (null == mEditor) {
            return;
        }
        SelectionModel model = mEditor.getSelectionModel();
        final String selectedText = model.getSelectedText();
        if (TextUtils.isEmpty(selectedText)) {
            return;
        }
        String toCamelCase = StringUtils.toCamelCase(selectedText);
        //将鼠标选中的转成驼峰
        DocumentUtil.writeInRunUndoTransparentAction(() -> {
            int selectionStart = mEditor.getSelectionModel().getSelectionStart();
            int selectionEnd = mEditor.getSelectionModel().getSelectionEnd();
            mEditor.getDocument().replaceString(selectionStart, selectionEnd, toCamelCase);
        });
    }


}
